-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Rework ForEachStmt Desugaring #86010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
elsakeirouz
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
elsakeirouz:rework-for-each-desugar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
+457
−533
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
elsakeirouz
commented
Dec 12, 2025
5381915 to
e7ec1ab
Compare
elsakeirouz
commented
Dec 13, 2025
elsakeirouz
commented
Dec 13, 2025
elsakeirouz
commented
Dec 13, 2025
| return ctx.getAsyncIteratorNext(); | ||
| } | ||
|
|
||
| static BraceStmt *desugarForEachStmt(ForEachStmt* stmt){ |
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially refactor this function to make it more readable.
0dc0a64 to
714b32f
Compare
I have not yet tested any behavior of generated SIL code but it seems reasonable in comparison. I was able to compile the standard library. Here: * include/swift/AST/ASTBridging.h, * include/swift/AST/Expr.h, * include/swift/AST/ExprNodes.def, * include/swift/AST/Stmt.h, * include/swift/AST/StmtNodes.def, * include/swift/AST/TypeCheckRequests.h, * include/swift/AST/TypeCheckerTypeIDZone.def, * include/swift/Sema/ConstraintLocator.h, * include/swift/Sema/SyntacticElementTarget.h, * lib/AST/ASTDumper.cpp, * lib/AST/ASTPrinter.cpp, * lib/AST/ASTScopeCreation.cpp, * lib/AST/ASTVerifier.cpp, * lib/AST/ASTWalker.cpp, * lib/AST/Bridging/StmtBridging.cpp, * lib/AST/Expr.cpp, * lib/AST/Stmt.cpp, * lib/AST/TypeCheckRequests.cpp, * lib/ASTGen/Sources/ASTGen/Stmts.swift, * lib/Parse/ParseStmt.cpp, * lib/SILGen/ASTVisitor.h, * lib/SILGen/SILGenExpr.cpp, * lib/SILGen/SILGenStmt.cpp, * lib/SILOptimizer/Mandatory/MoveOnlyDiagnostics.cpp, * lib/Sema/BuilderTransform.cpp, * lib/Sema/CSApply.cpp, * lib/Sema/CSDiagnostics.cpp, * lib/Sema/CSGen.cpp, * lib/Sema/CSSimplify.cpp, * lib/Sema/CSSyntacticElement.cpp, * lib/Sema/SyntacticElementTarget.cpp, * lib/Sema/TypeCheckEffects.cpp, * lib/Sema/TypeCheckStmt.cpp.
714b32f to
5285f67
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to lift the desugaring of ForEachStmt (when dealing with Sequences) from SILGen to the AST, and simultaneously delaying the synthesis of makeIterator and next calls until after typechecking.
The motivation for this is support for non-Copyable & non-Escapable sequences with non-Copyable elements in ForEachStmt. In order to know which variant of makeIterator and next calls we need, we must determine whether a Sequence is copyable, asynchronous, or neither. The compiler currently relies on the function choice to infer the type of the elements. However, to determine whether a Sequence is copyable, it must be typed.
For that reason, instead of matching the pattern type with the unwrapped result type of next(), we're now matching it with Sequence. This allows us to delay the decision and synthesis of those function calls until later.
Additionally, in order to avoid having to translate the rest of the complex desugar logic for the borrowing ForEachStmt in SILGen, we chose to represent it directly at the AST level, which SILGen will then use to emit the corresponding SIL code.
To make things simpler all around, we chose to start by reworking this pass for the regular/async ForEachStmts before handling borrowing statements.
We expect to be able to remove ConstraintKind::ValueWitness with this work.